package com.zscat.mallplus.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.oms.service.IOmsOrderService;
import com.zscat.mallplus.oms.vo.OrderParam;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.entity.PmsProductCategory;
import com.zscat.mallplus.pms.service.IPmsProductCategoryService;
import com.zscat.mallplus.pms.service.IPmsProductService;
import com.zscat.mallplus.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: rwz
 * @Date: 2020/10/07/10:29 下午
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "MarketIntegralController", description = "积分商城")
@RequestMapping("/api/market/integral")
public class MarketIntegralController {

    @Resource
    private IPmsProductService productService;

    @Resource
    private IPmsProductCategoryService productCategoryService;

    @Resource
    private IOmsOrderService orderService;

    @IgnoreAuth
    @ApiOperation(value = "查询商品分类")
    @SysLog(MODULE = "integral", REMARK = "查询商品分类")
    @GetMapping(value = "catatoryList")
    public Object catatoryList() {
        List<PmsProductCategory> productCategoryList = new ArrayList<PmsProductCategory>();
        List<PmsProductCategory> productCategories = productCategoryService.list(new QueryWrapper<PmsProductCategory>());
        for (PmsProductCategory productCategory : productCategories) {
            int count = productService.count(new QueryWrapper<PmsProduct>().eq("product_category_id", productCategory.getId()));
            if (count > 0) {
                productCategoryList.add(productCategory);
            }
        }
        return new CommonResult().success(productCategoryList);
    }

    @ApiOperation(value = "积分兑换")
    @SysLog(MODULE = "integral", REMARK = "积分兑换")
    @GetMapping(value = "exchange")
    public Object exchange(OrderParam orderParam) {
        return orderService.integralExchange(orderParam);
    }
}
